<template>
  <div class="app-container">
    <el-form :model="searchForm" ref="searchFormRef" inline v-show="showSearch">
      <el-form-item label="登录地址" prop="like.ipaddr">
        <el-input v-model="searchForm.like.ipaddr" placeholder="请输入登录地址" clearable class="w-140px" />
      </el-form-item>
      <el-form-item label="用户名称" prop="like.login_name">
        <el-input v-model="searchForm.like.login_name" placeholder="请输入用户名称" clearable class="w-140px" />
      </el-form-item>
      <el-form-item label="状态" prop="where.status">
        <el-select v-model="searchForm.where.status" placeholder="登录状态" clearable class="w-100px">
          <el-option v-for="dd in sys_common_status" :key="dd.value" :label="dd.label" :value="dd.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="登录时间" prop="time.create_time">
        <el-date-picker
          v-model="searchForm.time.create_time"
          style="width: 240px"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="search">搜索</el-button>
        <el-button icon="Refresh" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="reset mb-8px">
      <el-button
        type="danger"
        plain
        icon="Delete"
        :disabled="!ids.length"
        @click="handleDelete"
        v-hasPerm="['system:login-log:delete']"
        >删除</el-button
      >
      <el-button type="danger" plain icon="Close" @click="handleClean" v-hasPerm="['system:login-log:clear']"
        >清空</el-button
      >
      <el-button
        type="primary"
        plain
        icon="Unlock"
        :disabled="ids.length !== 1"
        @click="handleUnlock"
        v-hasPerm="['system:login-log:unlock']"
        >登出</el-button
      >
      <right-toolbar v-model:showSearch="showSearch" @refresh="getList" />
    </div>
    <el-table v-loading="loading" :data="listData" @selection-change="handleSelection" @sort-change="handleSort">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="编号" width="60" align="center" prop="id" />
      <el-table-column
        label="用户名称"
        min-width="100"
        align="center"
        prop="login_name"
        show-overflow-tooltip
        sortable="custom" />
      <el-table-column label="登录地址" align="center" prop="ipaddr" width="130" show-overflow-tooltip />
      <el-table-column label="登录地点" align="center" prop="location" show-overflow-tooltip />
      <el-table-column label="浏览器" align="center" prop="browser" show-overflow-tooltip />
      <el-table-column label="平台" align="center" prop="platform" show-overflow-tooltip />
      <el-table-column label="操作系统" align="center" prop="os" />
      <el-table-column label="登录状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_common_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作信息" align="center" prop="msg" show-overflow-tooltip />
      <el-table-column label="登录日期" align="center" prop="create_time" sortable="custom" width="140">
        <template #default="scope">{{ parseTime(scope.row.create_time) }}</template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="searchForm.pageNum"
      v-model:limit="searchForm.pageSize"
      @pagination="getList" />
  </div>
</template>
<script setup name="Logininfor">
  import { listLoginLogApi, delLoginLogApi, clearLoginLogApi, unlockLoginApi } from '@/api/system/login-log'

  const { proxy } = getCurrentInstance()
  const data = reactive({
    loading: true,
    ids: [],
    showSearch: true,
    total: 0,
    listData: [],
    searchForm: {
      pageNum: 1,
      pageSize: 10,
      like: { login_name: undefined, ipaddr: undefined },
      where: { status: undefined },
      time: { create_time: undefined },
      orderByColumn: 'id',
      orderBySort: 'desc'
    }
  })

  const { loading, ids, showSearch, total, listData, searchForm } = toRefs(data)
  const { sys_common_status } = proxy.useDict('sys_common_status')

  onMounted(() => getList())

  function getList() {
    loading.value = true
    listLoginLogApi(searchForm.value).then(res => {
      listData.value = res?.list || []
      total.value = res?.total || 0
      loading.value = false
    })
  }

  function search() {
    searchForm.value.pageNum = 1
    getList()
  }

  const searchFormRef = useTemplateRef('searchFormRef')
  function reset() {
    searchFormRef.value.resetFields()
    search()
  }

  function handleSelection(selection) {
    ids.value = selection.map(item => item.id)
  }
  function handleSort(column) {
    searchForm.value.orderByColumn = column.prop || ''
    searchForm.value.orderBySort = column.order || ''
    getList()
  }

  /** 删除按钮操作 */
  function handleDelete(row) {
    const infoIds = row?.id || ids.value.join(',')
    if (!infoIds) return proxy.$modal.msgWarning('未选择数据')
    proxy.$modal
      .confirm('是否确认删除访问编号为"' + infoIds + '"的数据项？')
      .then(function () {
        return delLoginLogApi(infoIds)
      })
      .then(() => {
        getList()
        proxy.$modal.msgSuccess('删除成功')
      })
      .catch(() => {})
  }
  /** 清空按钮操作 */
  function handleClean() {
    proxy.$modal
      .confirm('是否确认清空所有登录日志数据项？')
      .then(function () {
        return clearLoginLogApi()
      })
      .then(() => {
        getList()
        proxy.$modal.msgSuccess('清空成功')
      })
      .catch(() => {})
  }
  /** 登出按钮操作 */
  function handleUnlock() {
    const id = ids.value.length ? ids.value[0] : ''
    if (!id) return proxy.$modal.msgWarning('未选择数据')

    proxy.$modal
      .confirm('是否确认登出访问编号"' + id + '"用户?')
      .then(function () {
        return unlockLoginApi(id)
      })
      .then(() => {
        proxy.$modal.msgSuccess('登出成功')
      })
      .catch(() => {})
  }
</script>
